package com.example.demo08;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.edroid.common.utils.FileUtils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LoginPage extends Fragment implements OnClickListener {
	TextView editText;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_login, container, false);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		
		view.findViewById(R.id.btnLogin).setOnClickListener(this);
		view.findViewById(R.id.btnNoLogin).setOnClickListener(this);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
	
	void login(String username, String pwd) {
//		String ret = HttpRequestUtil.post(UrlManager.URL_LOGIN, 
//				"action=login&username=" + username + "&pwd=" + pwd);
//		
//		if(!TextUtils.isEmpty(ret)) {
//			System.out.println(ret);
//		}
		
		getActivity().runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				Toast.makeText(getActivity(), "登陆成功！", Toast.LENGTH_SHORT).show();
				((MainActivity)getActivity()).replace(new InfoPage());
			}
		});
	}
	
	final String F_SHORTCUTS = "/sdcard/apkhooker/files/shortcuts";
	
	void loadShortcut(ListView lv) {
		if(FileUtils.isEmptyFile(F_SHORTCUTS))
			return;
		
		try {
			JSONArray array = new JSONArray(FileUtils.fileToString(F_SHORTCUTS));
			List<String> itmes = new ArrayList<String>();
			
			for(int N=array.length(), i=0; i<N; i++) {
				JSONObject obj = array.getJSONObject(i);
				itmes.add(obj.getString("name"));
			}

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
					android.R.layout.simple_list_item_1,
					itmes);
			lv.setAdapter(adapter);
			lv.setTag(array);
			lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					System.out.println("-----------------------");
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	void showTb() {
		LayoutInflater inflater = getActivity().getLayoutInflater();
		
		View toolbarView = inflater.inflate(R.layout.float_script_recoder, null);
        WindowManager.LayoutParams lpd_params = new WindowManager.LayoutParams(
        		WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, 
                10, 10, 
                WindowManager.LayoutParams.TYPE_PHONE, 
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,//WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE /*| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL*/, 
                PixelFormat.TRANSPARENT);

        lpd_params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;

        WindowManager mWindowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        mWindowManager.addView(toolbarView, lpd_params);
		
//		FloatView fv = FloatViewManager.getInstance().create(mContext, R.layout.float_script_recoder, 60,100, null);
		
        toolbarView.findViewById(R.id.btn1).setOnClickListener(this);
		toolbarView.findViewById(R.id.btn2).setOnClickListener(this);
		toolbarView.findViewById(R.id.btn3).setOnClickListener(this);
		toolbarView.findViewById(R.id.btn4).setOnClickListener(this);
		
		final ListView mListView = (ListView) toolbarView.findViewById(R.id.list);
//		final View expand = toolbarView.findViewById(R.id.list);
//		toolbarView.findViewById(R.id.btnExpand).setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				if(expand.getVisibility() == View.VISIBLE) {
//					expand.setVisibility(View.GONE);
//				} else {
//					expand.setVisibility(View.VISIBLE);
//					loadShortcut(mListView);
//				}
//			}
//		});
		loadShortcut(mListView);
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btnLogin) {
			final String name = ((EditText)getView().findViewById(R.id.editUsername)).getText().toString();
			final String pwd = ((EditText)getView().findViewById(R.id.editPwd)).getText().toString();
			
			if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd)) {
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						login(name, pwd);
					}
				}).start();
			}
		} else if(v.getId() == R.id.btnNoLogin) {
//			((MainActivity)getActivity()).replace(new InfoPage());
			showTb();
		}
	}
}
